package problems.daily.y2022m8;

/**
 * 622. 设置循环队列
 * https://leetcode.cn/problems/design-circular-queue/
 * @author habitplus
 * @since 2022/8/2 0:57
 */
public class D20220802_622 {
}

class MyCircularQueue {

    int[] data;
    // 指向队首
    int front;
    // 指向队尾
    int tail;

    int size;

    public MyCircularQueue(int k) {
        front = tail = 0;
        size = k;
        if (k > 0) {
            data = new int[k + 1];
        }
    }

    public boolean enQueue(int value) {
        if (!isFull()) {
            data[tail] = value;
            tail = (tail + 1) % data.length;
            return true;
        }
        return false;
    }

    // 出队
    public boolean deQueue() {
        if (!isEmpty()) {
            front = (front + 1) % data.length;
            return true;
        }
        return false;
    }

    public int Front() {
        if (isEmpty()) return -1;
        return data[front];
    }

    public int Rear() {
        if (isEmpty()) return -1;
        if (tail - 1 >= 0) return data[tail - 1];
        else return data[data.length + tail - 1];
    }

    public boolean isEmpty() {
        if (size > 0) {
            return tail == front;
        }
        return true;
    }

    public boolean isFull() {
        if (size > 0) {
            return (tail + 1) % data.length == front;
        }
        return true;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */
